package com.sdry.web.controller.zc.quality;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Part;
import com.sdry.model.llm.User;
import com.sdry.model.workplace.MousseBundleSample;
import com.sdry.model.zc.ZcMsSpotCheckDetailsEntity;
import com.sdry.model.zc.ZcMsSpotCheckEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.service.zc.ZcMsSpotCheckService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcMsSpotCheckController   
 * @Description:  膜丝抽检
 * @Author:       zc   
 * @CreateDate:   2018年10月18日 下午2:20:29   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("msSpotCheck")
public class ZcMsSpotCheckController {

	@Resource
	private ZcMsSpotCheckService zcMsSpotCheckService;
	/**
	 * 进入膜丝抽检页面
	 * @return
	 */
	@RequestMapping("enterMsSpotCheckPage")
	public String enterMsSpotCheckPage(){
		return "/zc/quality/msSpotCheck";
	}
	/**
	 * 查询膜丝抽检单列表
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectMsSpotCheckList")
	public void selectMsSpotCheckList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		if(zcQueryCriteria.getKeyword()==null){
			zcQueryCriteria.setKeyword("");
		}
		List<ZcMsSpotCheckEntity> selectMsSpotCheckList = zcMsSpotCheckService.selectMsSpotCheckList(zcQueryCriteria);
		int count = zcMsSpotCheckService.countMsSpotCheckList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectMsSpotCheckList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存膜丝抽检单
	 * @param request
	 * @param response
	 * @param zcMsSpotCheckEntity
	 */
	@RequestMapping("insertMsSpotCheck")
	public void insertMsSpotCheck(HttpServletRequest request,HttpServletResponse response,ZcMsSpotCheckEntity zcMsSpotCheckEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcMsSpotCheckEntity.setCheckStatus("0");
		zcMsSpotCheckEntity.setCheckPerson(userName);
		
		int affact = zcMsSpotCheckService.insertMsSpotCheck(zcMsSpotCheckEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 批量删除膜丝抽检单
	 * @param request
	 * @param response
	 * @param idArr
	 */
	@RequestMapping("delMsSpotCheckArr")
	public void delMsSpotCheckArr(HttpServletRequest request, HttpServletResponse response,@RequestParam("idArr[]") String[] idArr){
		int affact = 0;
		for (String did : idArr) {
			long id = Long.parseLong(did);
			affact += zcMsSpotCheckService.delMsSpotCheckArr(id);
		}
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 编辑膜丝抽检单
	 * @param request
	 * @param response
	 * @param zcMsSpotCheckEntity
	 */
	@RequestMapping("updateMsSpotCheck")
	public void updateMsSpotCheck(HttpServletRequest request, HttpServletResponse response,ZcMsSpotCheckEntity zcMsSpotCheckEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcMsSpotCheckEntity.setCheckPerson(userName);
		int affact = zcMsSpotCheckService.updateMsSpotCheck(zcMsSpotCheckEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询膜丝抽检详情单
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectMsSpotCheckDetailsListByNumber")
	public void selectMsSpotCheckDetailsListByNumber(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcMsSpotCheckDetailsEntity> selectMsSpotCheckDetailsListByNumber = zcMsSpotCheckService.selectMsSpotCheckDetailsListByNumber(zcQueryCriteria);
		int count = zcMsSpotCheckService.countMsSpotCheckDetailsListByNumber(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectMsSpotCheckDetailsListByNumber, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存膜丝抽检单详情
	 * @param request
	 * @param response
	 * @param zcMsSpotCheckDetailsEntity
	 */
	@RequestMapping("insertMsSpotCheckDetails")
	public void insertMsSpotCheckDetails(HttpServletRequest request,HttpServletResponse response,ZcMsSpotCheckDetailsEntity zcMsSpotCheckDetailsEntity){
		int affact = zcMsSpotCheckService.insertMsSpotCheckDetails(zcMsSpotCheckDetailsEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过膜丝条码查询膜丝捆条码
	 * @param request
	 * @param response
	 * @param msCode
	 */
	@RequestMapping("selectMbCodeByMsCode")
	public void selectMbCodeByMsCode(HttpServletRequest request,HttpServletResponse response,String msCode){
		MousseBundleSample mousseBundleSample = zcMsSpotCheckService.selectMbCodeByMsCode(msCode);
		JSONObject json= JSONObject.fromObject(mousseBundleSample);  
		try {
			if(mousseBundleSample!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除抽检单详情
	 * @param request
	 * @param response
	 * @param idArr
	 */
	@RequestMapping("delMsSpotCheckArrDetails")
	public void delMsSpotCheckArrDetails(HttpServletRequest request, HttpServletResponse response,@RequestParam("idArr[]") String[] idArr){
		int affact = 0;
		for (String did : idArr) {
			long id = Long.parseLong(did);
			affact += zcMsSpotCheckService.delMsSpotCheckArrDetails(id);
		}
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询膜丝抽检统计表
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectMsSpotCheckStatisticsTable")
	public void selectMsSpotCheckStatisticsTable(HttpServletRequest request, HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcMsSpotCheckDetailsEntity> selectMsSpotCheckStatisticsTable = zcMsSpotCheckService.selectMsSpotCheckStatisticsTable(zcQueryCriteria);
		int count = zcMsSpotCheckService.countMsSpotCheckStatisticsTable(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectMsSpotCheckStatisticsTable, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 全查膜丝名称列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("selectMsNameList")
	public void selectMsNameList(HttpServletRequest request, HttpServletResponse response){
		List<Part> selectMsNameList = zcMsSpotCheckService.selectMsNameList();
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectMsNameList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过膜丝名称查询膜丝规格
	 * @param request
	 * @param response
	 */
	@RequestMapping("selectMsStandardListByName")
	public void selectMsStandardListByName(HttpServletRequest request, HttpServletResponse response,String msName){
		List<Part> selectMsStandardListByName = zcMsSpotCheckService.selectMsStandardListByName(msName);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectMsStandardListByName, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
